package com.atguigu.tingshu.album.service.impl;

import cn.hutool.json.JSONObject;
import com.atguigu.tingshu.album.mapper.*;
import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.model.album.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@SuppressWarnings({"all"})
public class BaseCategoryServiceImpl extends ServiceImpl<BaseCategory1Mapper, BaseCategory1> implements BaseCategoryService {

	@Autowired
	private BaseCategory1Mapper baseCategory1Mapper;

	@Autowired
	private BaseCategory2Mapper baseCategory2Mapper;

	@Autowired
	private BaseCategory3Mapper baseCategory3Mapper;

	@Autowired
	private BaseCategoryViewMapper baseCategoryViewMapper;

	@Autowired
	private BaseAttributeMapper baseAttributeMapper;


	@Override
	public List<JSONObject> getBaseCategoryList() {

		ArrayList<JSONObject> categoryList = new ArrayList<>();
		List<BaseCategoryView> allCategoryList = baseCategoryViewMapper.selectList(null);
		Map<Long, List<BaseCategoryView>> category1Map = allCategoryList.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));

		for (Map.Entry<Long, List<BaseCategoryView>> entry1 : category1Map.entrySet()) {
			Long category1Id = entry1.getKey();
			String category1Name = entry1.getValue().get(0).getCategory1Name();
			JSONObject jsonObject1 = new JSONObject();
			jsonObject1.put("categoryId", category1Id);
			jsonObject1.put("categoryName", category1Name);
			ArrayList<JSONObject> category2List = new ArrayList<>();
			Map<Long, List<BaseCategoryView>> category2Map = entry1.getValue().stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
			for (Map.Entry<Long, List<BaseCategoryView>> entry2 : category2Map.entrySet()) {
				Long category2Id = entry2.getKey();
				String category2Name = entry2.getValue().get(0).getCategory2Name();
				JSONObject jsonObject2 = new JSONObject();
				jsonObject2.put("categoryId", category2Id);
				jsonObject2.put("categoryName", category2Name);
				List<JSONObject> category3List = entry2.getValue().stream().map(category3 -> {
					JSONObject jsonObject3 = new JSONObject();
					jsonObject3.put("categoryId", category3.getCategory3Id());
					jsonObject3.put("categoryName", category3.getCategory3Name());
					return jsonObject3;
				}).collect(Collectors.toList());
				jsonObject2.put("categoryChild", category3List);
				category2List.add(jsonObject2);
			}
			jsonObject1.put("categoryChild",category2List);
			categoryList.add(jsonObject1);
		}

		return categoryList;
	}

    @Override
    public List<BaseAttribute> getAttributeByCategory1Id(Long category1Id) {
        return baseAttributeMapper.getAttributeByCategory1Id(category1Id);
    }

    @Override
    public BaseCategoryView getCategoryView(Long category3Id) {
        return baseCategoryViewMapper.selectById(category3Id);
    }

    @Override
    public List<BaseCategory3> findTopBaseCategory3(Long category1Id) {
        return baseCategory3Mapper.findTopBaseCategory3(category1Id);
    }

	@Override
	public JSONObject getAllCategoryByCategory1Id(Long category1Id) {

		JSONObject resultMap = new JSONObject();
		LambdaQueryWrapper<BaseCategory2> category2queryWrapper = new LambdaQueryWrapper<>();
		category2queryWrapper.eq(BaseCategory2::getCategory1Id, category1Id);
		List<BaseCategory2> baseCategory2List = baseCategory2Mapper.selectList(category2queryWrapper);


		List<JSONObject> category2MapList = new ArrayList<>();
		for (BaseCategory2 baseCategory2 : baseCategory2List) {
			LambdaQueryWrapper<BaseCategory3> category3queryWrapper = new LambdaQueryWrapper<>();
			category3queryWrapper.eq(BaseCategory3::getCategory2Id,baseCategory2.getId());
			List<BaseCategory3> category3List = baseCategory3Mapper.selectList(category3queryWrapper);

			List<JSONObject> category3MapList = category3List.stream().map(category3 -> {
				JSONObject category3Map = new JSONObject();
				category3Map.put("categoryName", category3.getName());
				category3Map.put("categoryId", category3.getId());
				return category3Map;
			}).collect(Collectors.toList());

			JSONObject category2Map = new JSONObject();
			category2Map.put("categoryName",baseCategory2.getName());
			category2Map.put("categoryId", baseCategory2.getId());
			category2Map.put("categoryChild",category3MapList);

			category2MapList.add(category2Map);
		}

		BaseCategory1 baseCategory1 = baseCategory1Mapper.selectById(category1Id);
		resultMap.put("categoryName",baseCategory1.getName());
		resultMap.put("categoryId", baseCategory1.getId());
		resultMap.put("categoryChild",category2MapList);
		return resultMap;
	}


}
